package com.zhuangzhuang.dahua.controller;

import com.netsdk.lib.NetSDKLib;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class DahuaController {


    /**
     * 获取设备的链接列表
     *
     * @return
     */
    @GetMapping(value = "/api/dahua/index")
    public String index() {
        return "Hello Dahua";
    }

    @PostMapping("/api/dahua/menjin")
    public Map<String, Object> getDeviceInfo() {
        // 创建一个 Map 来存储设备信息
        Map<String, Object> deviceInfo = new HashMap<>();
        deviceInfo.put("deviceId", "DH123456");
        deviceInfo.put("deviceName", "Dahua Camera");
        deviceInfo.put("status", "online");
        deviceInfo.put("ipAddress", "192.168.1.100");
        return deviceInfo;
    }


    @PostMapping("/api/dahua/closeDoor")
    public Map<String, Object> closeDoor(@RequestParam("channelId") String channelId) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 尝试将 channelId 转换为整数
            int channel = Integer.parseInt(channelId);
            final NetSDKLib.NET_CTRL_ACCESS_CLOSE close = new NetSDKLib.NET_CTRL_ACCESS_CLOSE();
            close.nChannelID = channel;
            close.write();
            result.put("success", true);
            result.put("message", "门关闭操作成功");
        } catch (NumberFormatException e) {
            // 处理 channelId 无法转换为整数的情况
            result.put("success", false);
            result.put("message", "传入的 channelId 不是有效的数字，请检查输入");
        } catch (Exception e) {
            // 处理其他异常
            result.put("success", false);
            result.put("message", "门关闭操作失败: " + e.getMessage());
        }
        return result;
    }


}
